﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Linq;

namespace FacebookApp
{
	public class OrderDetailService
	{

		#region[OrderDetail_GetbyAll]
        public static List<OrderDetail> OrderDetail_GetByAll()
		{
            try
            {
                using (var dt = new DataClassesDataContext())
                {
                    var da = (from pu in dt.OrderDetails select pu).ToList();

                    return da;
                }
            }
            catch (Exception)
            {
                return null;
            }
		}
		#endregion
		#region[OrderDetail_GetbyId]
        public static OrderDetail OrderDetail_GetById(string Data)
		{
            try
            {
                using (var dt = new DataClassesDataContext())
                {
                    int id = int.Parse(Data);
                    var da = (from pu in dt.OrderDetails where pu.Id == id select pu).First();

                    return da;
                }
            }
            catch (Exception)
            {
                return null;
            }
		}

        public static OrderDetail OrderDetail_GetByOrderId(string Data)
        {
            try
            {
                using (var dt = new DataClassesDataContext())
                {
                    int id = int.Parse(Data);
                    var da = (from pu in dt.OrderDetails where pu.Order_Id == id select pu).First();

                    return da;
                }
            }
            catch (Exception)
            {
                return null;
            }
        }

		#endregion
		#region[OrderDetail_GetbyTop]
        public static List<OrderDetail> OrderDetail_GetByTop(string Top, string Where, string Order)
		{
            try
            {
                using (var dt = new DataClassesDataContext())
                {
                    string query = "Select * from [OrderDetail]";
                    if (!Top.Equals(""))
                        query = " Select top " + Top + " * from [OrderDetail] ";
                    if (!Where.Equals(""))
                        query += " where " + Where + " ";
                    if (!Order.Equals(""))
                        query += " order by " + Order;

                    IEnumerable<OrderDetail> results = dt.ExecuteQuery<OrderDetail>(@query);

                    return results.ToList();
                }
            }
            catch (Exception e)
            {
                return null;
            }
		}
		#endregion
		#region[OrderDetail_GetbyEbook_Id]
		public DataTable OrderDetail_GetByEbook_Id(string Ebook_Id)
		{
            //using (var cmd = new SqlCommand("sp_OrderDetail_GetByEbook_Id", GetConnection()))
            //{
            //    cmd.CommandType = CommandType.StoredProcedure;
            //    cmd.Parameters.Add(new SqlParameter("@Ebook_Id", Ebook_Id));
            //    var da = new SqlDataAdapter(cmd);
            //    var dt = new DataTable();
            //    da.Fill(dt);
            //    return dt;
            //}
		    return null;
		}
		#endregion
		#region[OrderDetail_GetbyOrder_Id]
		public DataTable OrderDetail_GetByOrder_Id(string Order_Id)
		{
            //using (var cmd = new SqlCommand("sp_OrderDetail_GetByOrder_Id", GetConnection()))
            //{
            //    cmd.CommandType = CommandType.StoredProcedure;
            //    cmd.Parameters.Add(new SqlParameter("@Order_Id", Order_Id));
            //    var da = new SqlDataAdapter(cmd);
            //    var dt = new DataTable();
            //    da.Fill(dt);
            //    return dt;
            //}
		    return null;
		}
		#endregion
		#region[OrderDetail_Insert]
        public static void OrderDetail_Insert(OrderDetailInfo Data)
		{
            try
            {
                using (var dt = new DataClassesDataContext())
                {
                    OrderDetail ob = new OrderDetail();
                    ob.Order_Id = long.Parse(Data.Order_Id);
                    ob.Ebook_Id = long.Parse(Data.Ebook_Id);
                    ob.Quantity = int.Parse(Data.Quantity);
                    ob.Money = decimal.Parse(Data.Money);

                    dt.OrderDetails.InsertOnSubmit(ob);
                    dt.SubmitChanges();
                }
            }
            catch (Exception)
            {

            }
		}
		#endregion
		#region[OrderDetail_Update]
        public static void OrderDetail_Update(OrderDetailInfo Data)
		{
            try
            {
                using (var dt = new DataClassesDataContext())
                {
                    int id = int.Parse(Data.Id);
                    //Author da = (from au in dt.Authors where au.Id == id select au).First();
                    OrderDetail ob = dt.OrderDetails.Single(au => au.Id == id);
                    ob.Order_Id = long.Parse(Data.Order_Id);
                    ob.Ebook_Id = long.Parse(Data.Ebook_Id);
                    ob.Quantity = int.Parse(Data.Quantity);
                    ob.Money = decimal.Parse(Data.Money);

                    dt.SubmitChanges();
                }
            }
            catch (Exception)
            {

            }
		}
		#endregion
		#region[OrderDetail_Delete]
		public static void OrderDetail_Delete(string Data)
		{
            try
            {
                using (var dt = new DataClassesDataContext())
                {
                    int id = int.Parse(Data);
                    //Author da = (from au in dt.Authors where au.Id == id select au).First();
                    OrderDetail da = dt.OrderDetails.Single(au => au.Id == id);
                    dt.OrderDetails.DeleteOnSubmit(da);

                    dt.SubmitChanges();
                }
            }
            catch (Exception)
            {

            }
		}
		#endregion
	}

    public class OrderDetailInfo
    {
        public string Id { get; set; }
        public string Order_Id { get; set; }
        public string Ebook_Id { get; set; }
        public string Quantity { get; set; }
        public string Money { get; set; }

    }
}